window.addEventListener('load', function () {
    // 轮播图
    var main_focus = document.querySelector('.main_focus');
    var prev = document.querySelector('.slider_control_prev');
    var next = document.querySelector('.slider_control_next');
    var indicators = document.querySelector('.slider_indicators').children[0];
    var focus = document.querySelector('.slider_focus').children[0];
    for (var i = 0; i < focus.children.length; i++) {
        var li = document.createElement('li');
        li.setAttribute('data-index', i);
        indicators.appendChild(li);
        li.addEventListener('click', function () {
            for (var i = 0; i < indicators.children.length; i++) {
                indicators.children[i].className = '';
            }
            this.className = 'current';
            var target = -this.getAttribute('data-index') * main_focus.offsetWidth;
            animate(focus, target);
            num = -this.getAttribute('data-index');
            circle = this.getAttribute('data-index');
        })
    }
    indicators.children[0].className = 'current';
    main_focus.addEventListener('mouseenter', function () {
        prev.style.display = 'block';
        next.style.display = 'block';
        clearInterval(banner);
        banner = null;
    })
    main_focus.addEventListener('mouseleave', function () {
        prev.style.display = 'none';
        next.style.display = 'none';
        banner = setInterval(function () {
            next.click();
        }, 2000)
    })
    var num = 0;
    var flag = true;
    var circle = 0;
    focus.appendChild(focus.children[0].cloneNode(true));
    prev.addEventListener('click', function () {
        if (flag) {
            flag = false;
            if (num == 0) {
                num = -(focus.children.length - 1);
                focus.style.left = num * main_focus.offsetWidth + 'px';
            }
            num++;
            var target = num * main_focus.offsetWidth;
            animate(focus, target, function () {
                flag = true;
            });
            circle--;
            if (circle == -1) {
                circle = indicators.children.length - 1;
            }
            for (var i = 0; i < indicators.children.length; i++) {
                indicators.children[i].className = '';
            }
            indicators.children[circle].className = 'current';
        }
    })
    next.addEventListener('click', function () {
        if (flag) {
            flag = false;
            if (num == -(focus.children.length - 1)) {
                num = 0;
                focus.style.left = 0;
            }
            num--;
            var target = num * main_focus.offsetWidth;
            animate(focus, target, function () {
                flag = true;
            });
            circle++;
            if (circle == indicators.children.length) {
                circle = 0;
            }
            for (var i = 0; i < indicators.children.length; i++) {
                indicators.children[i].className = '';
            }
            indicators.children[circle].className = 'current';
        }
    })
    var banner = setInterval(function () {
        next.click();
    }, 2000)
});
$(function () {
    // 页面滚动，秒杀和电梯导航的显示与隐藏
    fixedtool();

    function fixedtool() {
        if ($(document).scrollTop() >= $(".recommend").offset().top) {
            $(".fixedtool_right").fadeOut();
        } else {
            $(".fixedtool_right").fadeIn();
        };
        if ($(document).scrollTop() >= $(".recommend").offset().top) {
            $(".fixedtool_left").fadeIn();
        } else {
            $(".fixedtool_left").fadeOut();
        };
    };
    var flag = true;
    $(window).scroll(function () {
        fixedtool();
        // 滚动不同位置显示不同电梯导航
        if (flag) {
            $(".floor>div").each(function (i, ele) {
                if ($(document).scrollTop() >= $(ele).offset().top) {
                    $(".fixedtool_left li").eq(i).addClass("fixedtool_current").siblings().removeClass("fixedtool_current");
                };
            });
        }
    });
    // 电梯导航
    $(".fixedtool_left li").click(function () {
        flag = false;
        var top = $(".floor>div").eq($(this).index()).offset().top;
        $("body, html").stop().animate({
            scrollTop: top + 'px'
        }, function () {
            flag = true;
            console.log($(document).scrollTop());
            console.log(12);
        });
        $(this).addClass("fixedtool_current").siblings().removeClass();
    });
});